package com.mathdroid.model.enums;

import java.util.LinkedList;

import com.mathdroid.exceptionhandling.ParserException;

/**
 * Each item has one type. This has nothing to do with the slot they fit in or
 * if the character can wear it.
 */
public enum EItemType {
	
	/**
	 * Armor items.
	 */
	ARMOR("Armor"),
	/**
	 * Weapon items.
	 */
	WEAPON("Weapon"),
	/**
	 * Module items.
	 */
	MODULE("Module");

	
	/**
	 * String representation of the type.
	 */
	private String name = null;
	
	/**
	 * Create a new item type enumeration.
	 * @param n Name of the type.
	 */
	private EItemType(final String n) {
		this.name = n;
	}
	
	@Override	
	public String toString() {
		return this.name;
	}
	
	/**
	 * Get a list of all item types.
	 * @return Item types.
	 */
	public static final LinkedList<EItemType> getItemTypes() {
		LinkedList<EItemType> types = new LinkedList<EItemType>();
		
		types.add(ARMOR);
		types.add(WEAPON);
		types.add(MODULE);
		
		return types;
	}
	
	/**
	 * Parse a string to an enumeration object. The parser compares the
	 * string representation of this enumeration with the parameter, not the
	 * literal.
	 * @param s String.
	 * @return EItemType object.
	 * @throws ParserException The string cannot be parsed.
	 */
	public static final EItemType parseEItemType(final String s) 
	throws ParserException {
		EItemType t = null;
		
		for (EItemType type : EItemType.getItemTypes()) {
			if (s.equals(type.toString())) {
				t = type;
			}
		}
		
		if (t == null) {
			ParserException p = new ParserException("No such item type.");
			p.fillInStackTrace();
			throw p;
		}
		
		return t;
	}
}
